package com.yytek.nmp.service.impl;

import com.yytek.common.core.domain.DeviceCache;
import com.yytek.nmp.api.RemoteJobService;
import com.yytek.nmp.api.domain.SysJob;
import com.yytek.nmp.domain.IotDeviceInfo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class IotDeviceTaskServiceImpl {
    @Autowired
    private RemoteJobService remoteJobService;

    /**
     * 绑定设备任务模板创建设备任务
     * @param deviceInfo
     */
    public void createDeviceTaskJob(IotDeviceInfo deviceInfo) {
        remoteJobService.createDeviceTaskJob(deviceInfo.getProjectId(),deviceInfo.getProductId(),deviceInfo.getIotId());
    }

    /**
     * 删除设备任务
     * @param iotId
     */
    public void delteDeviceJob(String iotId) {
        remoteJobService.delteDeviceJob(iotId);
    }

    /**
     * 更改设备任务状态
     * @param list
     */
    public void changeDeviceTask(List<DeviceCache> list) {
        List<SysJob> sysJobList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DeviceCache deviceCache = list.get(i);
            String iotId = deviceCache.getIotId();
            //设备状态(1-未激活 2-在线 3-离线)
            String deviceStatus = deviceCache.getConnectStatus().toString();
            //任务状态（0正常 1暂停）
            String taskStatus = "0";
            if(deviceStatus.equals("3")){
                taskStatus = "1";
            }
            //更改除状态任务的 所有设备任务
            SysJob sysJob = new SysJob();
            sysJob.setDeviceId(iotId);
            sysJob.setStatus(taskStatus);
            sysJobList.add(sysJob);
        }
        //任务状态（0正常 1暂停）
        remoteJobService.changeDeviceNoStateTask(sysJobList);
    }
}
